package com.hex.ds.hdtp.core.inf.common.exception;

import com.hex.ds.hdtp.core.inf.common.enums.SourceJdbcErrorCode;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.*;

/**
 * @Package: com.hex.ds.hdtp.core.inf.common.exception
 * @ClassName SourceException
 * @Description: 数据源操作异常
 * @Author: wz.li
 * @Date 2023/2/6 15:13
 * @Version v2.0.2
 **/
@Data
@Accessors(chain = true)
public class SourceJDBCException extends RuntimeException{
    private String code;
    private String errMsg;
    // SQL是否执行成功
    private boolean executionSuccess;
    // 执行总SQL数量
    private int totalSqlCount;
    // 执行成功的SQL数量
    private int successCount;
    // 执行失败的SQL数量
    private int failureCount;
    // 执行的SQL段
    private ArrayList<SourceJDBCExceptionDetail> jdbcDetail;

    public SourceJDBCException() {
        executionSuccess = false;
        totalSqlCount = 0;
        successCount = 0;
        failureCount = 0;
        jdbcDetail = new ArrayList<>();
    }

    public void incrementSuccessCount() {
        this.successCount++;
    }

    public void incrementFailureCount() {
        this.failureCount++;
    }

    /**
     * @Method SourceJDBCException
     * @Param message
     * @Param e
     * @Return
     * @Description 抛出异常
     * @Author gj.xu
     * @Date 2024/3/4 20:10
     * @Version V2.0
     */
    public SourceJDBCException(String message, Exception e) {
        super(message, e);
    }

    public SourceJDBCException(SourceJdbcErrorCode errorCode) {
        super(errorCode.getMessage());
        this.code = errorCode.getCode();
        this.errMsg = errorCode.getMessage();
    }

    public SourceJDBCException(SourceJdbcErrorCode errorCode, String errMsg) {
        this.code = errorCode.getCode();
        this.errMsg = errMsg;
    }

    /**
     * @Method SourceJDBCException
     * @Param errorCode
     * @Param e
     * @Return
     * @Description 构造异常
     * @Author gj.xu
     * @Date 2024/3/4 18:44
     * @Version V2.0
     */
    public SourceJDBCException(SourceJdbcErrorCode errorCode, Exception e) {
        super(e.getMessage(), e);
        this.code = errorCode.getCode();
        this.errMsg = errorCode.getMessage() + e.getMessage();
    }

    public SourceJDBCException(SourceJdbcErrorCode errorCode, Exception e, String errMsg) {
        super(errMsg, e);
        this.code = errorCode.getCode();
        this.errMsg = errMsg;
    }
}
